﻿using Microsoft.Office.Interop.Word;

namespace Header.Test
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var app = new Application();
            app.Visible = true;

            var bodyDoc = app.Documents.Open(@"C:\Users\陈城\source\repos\office-middleware\Header.Test\body.doc");
            var headerDoc = app.Documents.Open(@"C:\Users\陈城\source\repos\office-middleware\Header.Test\header.doc");
            bodyDoc.PageSetup.TopMargin = headerDoc.PageSetup.TopMargin;

            var bodySection = GetSection(bodyDoc);
            var headerSection = GetSection(headerDoc);

            bodySection.PageSetup.DifferentFirstPageHeaderFooter = headerSection.PageSetup.DifferentFirstPageHeaderFooter;
            bodySection.PageSetup.OddAndEvenPagesHeaderFooter = headerSection.PageSetup.OddAndEvenPagesHeaderFooter;
            bodySection.PageSetup.TopMargin = headerSection.PageSetup.TopMargin;
            bodySection.PageSetup.BottomMargin = headerSection.PageSetup.BottomMargin;
            bodySection.PageSetup.LeftMargin = headerSection.PageSetup.LeftMargin;
            bodySection.PageSetup.RightMargin = headerSection.PageSetup.RightMargin;
            bodySection.PageSetup.HeaderDistance = headerSection.PageSetup.HeaderDistance;


            HeaderFooter bodyHeader = bodySection.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary];
            HeaderFooter headerHeader = headerSection.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary];
            bodyHeader.Range.ParagraphFormat.Borders[WdBorderType.wdBorderBottom].LineStyle = headerHeader.Range.ParagraphFormat.Borders[WdBorderType.wdBorderBottom].LineStyle;

            if (bodyHeader.Exists)
            {
                bodyHeader.Range.Delete();
            }
            if (headerHeader.Exists)
            {
                headerHeader.Range.Copy();
                bodyHeader.Range.PasteAndFormat(WdRecoveryType.wdFormatOriginalFormatting);
                headerDoc.Close();
                app.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;
                app.Selection.Find.Execute(FindText: "B", MatchCase: false, ReplaceWith: "替换", Replace: WdReplace.wdReplaceAll);
                app.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;

            }


            //foreach (Section section in bodyDoc.Sections)
            //{
            //    var header = section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary];
            //    if (header.Exists)
            //    {
            //        header.Range.Delete();
            //    }
            //    //section.PageSetup.DifferentFirstPageHeaderFooter = 1;
            //    //section.PageSetup.OddAndEvenPagesHeaderFooter = 1;
            //}


            Section GetSection(Document doc)
                => doc.Sections[1];

            //void PasetRange()
            //{
            //    foreach (HeaderFooter item in bodyDoc.Sections[1].Headers)
            //    {
            //        if (item.IsHeader)
            //        {

            //        }
            //    }
            //}


            Console.ReadKey();

            app.Quit(false);


        }
    }
}